﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Diagnostics;

namespace WhileAwayWpf
{
    /// <summary>
    /// Interaction logic for AdminWindow.xaml
    /// </summary>
    public partial class AdminWindow : Window
    {
        AwayMessageCollection messages;
        private Thread settingsMon;
        private EventWaitHandle settingsEvent = new EventWaitHandle(false, EventResetMode.AutoReset, "C4F-WhileAway-Settings");
		UserInfo userInfo;
		
        public AdminWindow()
        {
            InitializeComponent();

            LoadSettings();

			settingsMon = new Thread(MonitorMessages);
            settingsMon.IsBackground = true;
            settingsMon.Start();
        }

        void MonitorMessages()
        {
            // Loop waiting for reload flag
            while (settingsEvent.WaitOne())
            {
                // Triggers the ProgressChanged event which is on the UI dispatcher thread
                Dispatcher.Invoke((Action)(() => { LoadSettings(); }));
            }
        }

        private void LoadSettings()
        {
            userInfo = new UserInfo();
            userInfo.Load();
            this.DataContext = userInfo;

            TimeSpan span = userInfo.ReturnTime.Subtract(DateTime.Now);
            textBoxHours.Text = Math.Max(0, span.Hours).ToString();
            textBoxMinutes.Text = Math.Max(0, span.Minutes).ToString();

            messages = new AwayMessageCollection();
            messages.LoadFromDisk();
            listBoxMessages.ItemsSource = messages;
        }

        private void buttonLeave_Click(object sender, RoutedEventArgs e)
        {
            SaveSettings();
            
            Win32.ActivateScreensaver();
        }

        private void SaveSettings()
        {
            if (textBoxHours.Text == "0" && textBoxMinutes.Text == "0") userInfo.ReturnTime = DateTime.MinValue;
            else userInfo.ReturnTime = DateTime.Now.AddHours(int.Parse(textBoxHours.Text)).AddMinutes(int.Parse(textBoxMinutes.Text));

            userInfo.Save();
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            SaveSettings();
            this.Close();
        }

        private void buttonPhotoFolderBrowse_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
            dlg.SelectedPath = userInfo.PhotoLocation;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                userInfo.PhotoLocation = dlg.SelectedPath;
            }
        }

        private void buttonDeleteOneMessage_Click(object sender, RoutedEventArgs e)
        {
            int idx = listBoxMessages.SelectedIndex;
            if ( idx > -1)
            {
                messages.RemoveAt(idx);
                messages.Save();
            }
        }

        private void buttonDeleteAllMessages_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "Delete all messages", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                messages.Clear();
                messages.Save();
            }
        }
    }
}
